


/*
 * sc_p.c
 *
 * Created: 2012-07-09 07:09:24
 *  Author: Antrykot
 */ 


#define P_VER	0x0004

#include "../sc-p/sc-p.h"
#include "../sc-p/utils.h"
#include "../sc-p/trans-p.h"
#include "../sc-p/measure.h"
#include "uart.h"
extern u8 add_params_num ;					// set_add_data

u8 ibit = 0;

#ifdef MEDIA_CH4IR
u8 aquire = NORMAL_MODE;
u8 spec_aquire = NORMAL_MODE;
#endif


ISR(USART0__RX_vect)
{
	if (rx_len < 32)
	{
		r_buff[rx_len] = UDR0;
	}
	rx_len++;
	if (/*rx_len > 6 ||*/ r_buff[rx_len-1] == 0x0D)
	{
		UCSR0C = 0;
		UCSR0B = 0;
		aquire |= FINISH_MODE;
		LG_OFF;
		LR_OFF;
		set_clock_low(); 
		T2_HALT;
	}
}

ISR(USART0__TX_vect)
{
	while(cur_tx < tx_len)
	{
		UDR0 = t_buff[cur_tx];
		cur_tx++;
	}
}


ISR(TIMER0_OVF_vect)
{
	t0_cnt ++;
	
	if (t0_cnt > 150)	// 30ms (256us * 8div * 15)
	{
		t0_cnt = 0;
		timeout = 1;
		T0STOP;
		PTX_H;
	}
}


ISR(TIMER1_COMPA_vect)
{

	TR_P_DISABLE;

	
	send_command();
	timer_cnt++;
}


ISR(TIMER2_COMPA_vect)
{
	err++;
	switch(aquire)
	{
		case CALIB_FACT_WAIT:
			spec_aquire = CALIB_FACT;
		break;
		case CALIB_ZERO_WAIT:
			spec_aquire = CALIB_ZERO;
		break;
		case CALIB_SENSE_WAIT:
			spec_aquire = CALIB_SENSE;
		break;
		case ASKED_MODE:
			
			//aquire = NORMAL_MODE;
		break;
	}

	TR_P_DISABLE;

	send_command();


}

ISR(INT0_vect)
{
	trans();
	WAITMS(10);
	PTX_H;
	EIFR |= (1 << INTF0);
}



#ifdef MEDIA_CH4IR

u8 cal_now = 0;

int main(void)
{
	
	WAITMS(30);		
	setup_io();
	init_eeprom();
	DCDC_ON;
	WAITMS(300);
	
	search_eep();
	
	auto_range = 1;
		
		
	PTX_H;
		
	t_pom = (1 << 14) | 15;	
	
	OCR2A = 250;	//0,02s/(1/(8*10^6))/1024	
	//TCCR2B = 0x05;		// div 1024 
	TIMSK2 |= (1 << OCIE2A);
	
	//TCCR1A = 4;			// CTC
	TCCR1B = 5 | (1 << WGM12)	;			// div 1024;
	//OCR1A = 876;		// 1s (1000000 / 1024 )
	OCR1A = 438;		// xs
	TIMSK1 = (1 << OCIE1A);
	aquire = NORMAL_MODE;
	
	u16 old_pom = 0;
	
	sei();
	TR_P_ENABLE;
	
    while(1) 
    {
		TX_H;
		
		static u8 fast_measure_count = 0;		//
		
		if (aquire & FINISH_MODE)
		{
			set_clock_low();
			T2_HALT;
			TCNT1 = 0;
			recv_data();
		
			if (t_pom > old_pom + 30)
			{
				fast_measure_count = 30;
			}	
			if (fast_measure_count)
			{
				//TCNT1 = 438;			/// next measure in 0.25s
				fast_measure_count--;
			}
			
			old_pom = t_pom;
		
			TR_P_ENABLE;
			aquire = NORMAL_MODE;
		}

		calc_aku();
		calc_pow();
		calc_dc();
		
		WAITMS(50);
		
		//while(swl_check());
		

		if (timer_cnt > 3600)
		{
			timer_cnt = 0;
			timer_cnt_h++;
			write_word(EEP_WORK_TIME,timer_cnt_h);
		}
		
	}	
  
}
#endif


void setup_io()
{
	
	PORTA = 0;
//#ifndef MEDIA_CO2
	//DDRA = 0 | ( 1 << 0);	// DC/DC on off
//#else
	DDRD = 0 | ( 1 << 3) ;	// DC/DC on off, TX
//#endif
	PORTB = 0;
	
	DDRB = 0 | ( 1 << 0);	// DC/DC 5/100%
	DDRB |= ( 1 << 1);		// ADC CLK
	DDRB &= ~( 1 << 2);		// ADC DOUT
	DDRB |= ( 1 << 3);		// ADC DIN
	DDRB |= ( 1 << 4);		// ADC CS
	
	DDRB |= ( 1 << 5);		// YELLOW
	DDRB |= ( 1 << 6);		// RED
	DDRB |= ( 1 << 7);		// GREEN
	
	//DDRB |= (1 << 7);		// MIPEX_TX
	
	PORTD = 0;
	
	DDRD |=  (1 << 1);			// P_TX
	DDRD &= ~(1 << 0);			// P_RX
	DDRD |= (1 << 4);		// internal trans
	//DDRD |= (1 << 5);		// P_TX_(ver IR)
	//DDRD &= ~(1 << 4);		// P_RX_(ver IR)
	//DDRD |= (1 << 5);

	
	PORTD |= (1 << 4) | (1 << 2);			// RX set high, CLK set high
	
	
#ifndef MEDIA_CO2	
	EICRA = 0 | (1 << ISC01);		// INT0 & INT1 falling edge
#else
	EICRA = 0;		// low level of INT0
#endif		
	//TR_K_ENABLE;
	//TR_P_ENABLE;
		
	TCCR0B = 0 | ( 1 << CS01);		// T0, prescale 8
	TIMSK0 |= ( 1 << TOIE0);		// interrupt T0 overflow

	
	//PORTB |= (1 << 6);		// MIPEX_RX
}

#define DEF_EEPFLAG 0xA0


void init_eeprom()
{
	u8 x = _read(0);
	
	if (x != DEF_EEPFLAG)
	{
		swxa = 0.003907;
		swxb = 14.15;
		swxc = 17990;
		
		wxa = 0.003307;
		wxb = 14.15;
		wxc = 17990;
		
		snxa = 1.70766882;
		snxb = 1349;
		
		nxa = 1.70766882;
		nxb = 1349;

		x_scw = 2195;
		x_szw = 992;
		
		x_cw = 2195;
		x_zw = 992;
		

#ifdef MEDIA_CH4
		x_scn = 1900;
		x_szn = 800;
		x_cn = 1900;
		x_zn = 800;
		stn = 192;
#elif MEDIA_CON
		x_scn = 2200;
		x_szn = 580;
		x_cn = 2200;
		x_zn = 580;
		stn = 158;
#elif MEDIA_COS
		x_scn = 3550;
		x_szn = 660;
		x_cn = 3550;
		x_zn = 660;
		stn = 850;
#elif MEDIA_CO2
		Zero = 0.92;
		Span = 0.27;
		T_cal = 300;

		alpha = 0.672;
		beta = 0.283;
#endif

		x_aku = STD_AKU_MINUS;
		x_pow = STD_AKU_MINUS;


		stw1 = 300;
		stw2 = 852;

		write_word(EEP_PTR,EEP_PTR+2);

		write_double(EEP_SWXA,swxa);
		write_double(EEP_SWXB,swxb);
		write_double(EEP_SWXC,swxc);
		write_double(EEP_WXA,wxa);
		write_double(EEP_WXB,wxb);
		write_double(EEP_WXC,wxc);

		write_double(EEP_SNXA,snxa);
		write_double(EEP_SNXB,snxb);
		write_double(EEP_NXA,nxa);
		write_double(EEP_NXB,nxb);


		write_word(EEP_X_SZW,x_szw);
		write_word(EEP_X_SCW,x_scw);
		
		write_word(EEP_X_ZW,x_zw);
		write_word(EEP_X_CW,x_cw);
		
		write_word(EEP_X_SZN,x_szn);
		write_word(EEP_X_SCN,x_scn);
		
		write_word(EEP_X_ZN,x_zn);
		write_word(EEP_X_CN,x_cn);
		
		write_word(EEP_X_AKU,x_aku);
		write_word(EEP_X_POW,x_pow);

		write_word(EEP_STN,stn);
		write_word(EEP_STW1,stw1);
		write_word(EEP_STW2,stw2);


#ifdef MEDIA_CO2
		write_double(EEP_SZERO,Zero);
		write_double(EEP_ZERO,Zero);
		write_double(EEP_SSPAN,Span);
		write_double(EEP_SPAN,Span);
		write_double(EEP_STCAL,T_cal);
		write_double(EEP_TCAL,T_cal);
		write_double(EEP_SALPH,alpha);
		write_double(EEP_ALPH,alpha);
		write_double(EEP_SBETA,beta);
		write_double(EEP_BETA,beta);
#endif
		write_word(EEP_WORK_TIME,0);
		_write(0,DEF_EEPFLAG);
	}
	
	u16 a = read_word(EEP_WORK_TIME);
	if (a == 0xFFFF)
	{
		write_word(EEP_WORK_TIME,0);
	}
	
	swxa = read_double(EEP_SWXA);
	snxb = read_double(EEP_SWXB);
	swxc = read_double(EEP_SWXC);
	
	wxa = read_double(EEP_WXA);
	wxb = read_double(EEP_WXB);
	wxc = read_double(EEP_WXC);


	snxa = read_double(EEP_SNXA);
	snxb = read_double(EEP_SNXB);
	nxa = read_double(EEP_NXA);
	nxb = read_double(EEP_NXB);


	x_szw = read_word(EEP_X_SZW);
	x_scw = read_word(EEP_X_SCW);
	
	x_zw = read_word(EEP_X_ZW);
	x_cw = read_word(EEP_X_CW);
	
	x_szn = read_word(EEP_X_SZN);
	x_scn = read_word(EEP_X_SCN);
	
	x_zn = read_word(EEP_X_ZN);
	x_cn = read_word(EEP_X_CN);
	
	x_aku = read_word(EEP_X_AKU);
	x_pow = read_word(EEP_X_POW);
	
	
	stn = read_word(EEP_STN);
	stw1 = read_word(EEP_STW1);
	stw2 = read_word(EEP_STW2);
	
	timer_cnt_h = read_word(EEP_WORK_TIME);
}


/// gloabl
u16 eep_cnt = 0;
u16 time_cnt = 0;
u16 time_cnt_h = 0;

float m1,m2,m3,m4,m5,m6,m7,m8,m9;
u16 x0,x1,x2;
float w1,w2,w3,w4,w5;
float w_det;

double swxa;
double swxb;
double swxc;

double wxa;
double wxb;
double wxc;

double snxa;
double snxb;

double nxa;
double nxb;

u16 x_szw;
u16 x_scw;

u16 x_zw;
u16 x_cw;

u16 x_szn;
u16 x_scn;

u16 x_zn;
u16 x_cn;

u8 t_addr;
u16 t_data;
u16 t_pom;
u8 t_crc;

u8 r_addr;
u16 r_data;
u8 r_crc;

u8 tr_ok = 0;

u8 crc_data[10];
u8 crc_data_len;

#ifdef MEDIA_PS

float ap_a;
float ap_b;

#endif

#ifdef MEDIA_PD

float ap_a;
float ap_b;


#endif

